package com.zznode.redsun.commons.sercurity.model;

import java.util.Collection;
import java.util.Vector;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import com.zznode.csp.domain.basemodel.DomainBusiness;

@Entity
@DynamicInsert
@DynamicUpdate
@Table(name = "s_role")
public class Role extends DomainBusiness {
	public static final String ADMIN_ROLE_ID = "admin";

	private static final long serialVersionUID = 1L;

	@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
	@JoinTable(name = "s_role_permission", inverseJoinColumns = { @JoinColumn(name = "permission_id") }, joinColumns = { @JoinColumn(name = "role_id") })
	private Collection<Permission> permissions;

	public Collection<Permission> getPermissions() {
		return permissions;
	}

	public void setPermissions(Collection<Permission> permissions) {
		this.permissions = permissions;
	}

	public void addPermission(Permission permission) {
		if (null == permission) {
			return;
		}
		if (null == this.permissions) {
			this.permissions = new Vector<Permission>();
		}
		if (!this.permissions.contains(permission)) {
			this.permissions.add(permission);
		}
	}

	public void removePermission(Permission permission) {
		if (null == this.permissions || null == permission) {
			return;
		}
		if (this.permissions.contains(permission)) {
			this.permissions.remove(permission);
		}
	}
}
